<?php
	/**
	 * @ignore
	 */
	if(defined("apl_".__FILE__)) return;
	define("apl_".__FILE__,"apl_".__FILE__);

    require_once( APL_ROOT."/apl_constants.php");
/**
 * Date helper class
 *
 * @package apl
 * @subpackage helpers
 *
 */
class AplDateHelper{
	private $m;
	private $y;
	private $d;

	private $LocaleDays;
	private $LocaleMonths;

	public function __construct()
	{
		$this->LocaleDays     = Apl_Locales::getDays();
		$this->LocaleMonths   = Apl_Locales::getMonths();
		$this->setToday();
	}

	private function checkLeadingZeros()
	{
		if( !empty($this->d) )
		{
			if( strlen($this->d) == 1 )
				$this->d = "0".$this->d;
		}
		if( !empty($this->m) )
		{
			if( strlen($this->m) == 1 )
				$this->m = "0".$this->m;
		}
	}
	/**
	 * Sets the date from a string representation
	 * e.g 11/8/2006
	 *
	 * @param String $DateString
	 * @param String $Seperator
	 */
	public function setDateFromString( $DateString,$Seperator="." )
	{
		$date_array = explode($Seperator,$DateString );
		if( count($date_array) <> 3 )
			throw new Exception(" Invalid Date String",EX_PRM_NOT_VALID );
		foreach ($date_array as $val) {
			if( !is_numeric($val))
				throw new Exception(" Invalid data found in date parameter",EX_PRM_NOT_VALID );
		}
		$this->setDate($date_array[2],$date_array[1],$date_array[0]);
	}
	/**
	 * Sets date
	 *
	 * @param Integer $Year
	 * @param Integer $Month
	 * @param Integer $Day
	 */
	public function setDate( $Year,$Month,$Day)
	{
		$this->y = intval($Year);
		$this->m = intval($Month);
		$this->d = intval($Day);

		$this->checkLeadingZeros();
	}
	/**
	 * Sets date from a mysql date field value
	 *
	 * @param String $DateString : Database Column Value
	 * @param String $Seperator  : Date Sepereator ( Default : - )
	 */
	public function setDateFromMySqlDate( $DateString,$Seperator="-" )
    {
        $date_array = explode($Seperator,$DateString );
        if( count($date_array) <> 3 )
            throw new Exception(" Invalid Date String",EX_PRM_NOT_VALID );
        foreach ($date_array as $val) {
            if( !is_numeric($val))
                throw new Exception(" Invalid data found in date parameter",EX_PRM_NOT_VALID );
        }
        $this->setDate($date_array[0],$date_array[1],$date_array[2]);
    }
	/**
	 * Sets date from unixtime stamp
	 *
	 * @param unknown $uxTimeStamp
	 */
	public function setDateFromUnixTime( $uxTimeStamp )
	{
		$NewDate = getdate( $uxTimeStamp );
		$this->y = $NewDate["year"];
		$this->m = $NewDate["mon"];
		$this->d = $NewDate["mday"];
		$this->checkLeadingZeros();
	}
	/**
	 * Set activedate as today
	 *
	 */
	public function setToday()
	{
		$this->y = date("Y");
		$this->m = date("m");
		$this->d = date("d");

		$this->checkLeadingZeros();
	}
	/**
	 * returns a valid string for a MySQL query
	 *
	 * @return String
	 */
	public function getMySqlQueryDate()
	{
		return $this->y."-".$this->m."-".$this->d;
	}

	/**
	 * Adds days to active date
	 *
	 * @param Integer $daysToAdd
	 */
	public function addDays($daysToAdd )
	{
		if(!is_numeric($daysToAdd))
			throw new Exception("Numeric value expected but something different found!!",EX_PRM_NOT_VALID );

		$NewDate = getdate(
							mktime(0,0,0,$this->m,$this->d+$daysToAdd,$this->y)
						  );
		$this->y = $NewDate["year"];
		$this->m = $NewDate["mon"];
		$this->d = $NewDate["mday"];

		$this->checkLeadingZeros();
	}
	/**
	 * Returns a unixtime stamp representation of active date
	 *
	 * @return unknown
	 */
	public function getUnixTime()
	{
		return mktime(0,0,0,$this->m,$this->d,$this->y);
	}
	/**
	 * returns the day (1-31)
	 *
	 * @return Integer
	 */
	public function day()
	{
		return $this->d;
	}
	/**
	 * Returns the months (1-12)
	 *
	 * @return Integer
	 */
	public function month()
	{
		return $this->m;
	}
	/**
	 * Returns year
	 *
	 * @return Integer
	 */
	public function year()
	{
		return $this->y;
	}
	/**
	 * returns the last day of the active date
	 *
	 * @returnInteger
	 */
	public function getLastDay()
	{
		for( $i = 26; $i<32;$i++ )
		{
			if( !checkdate($this->m,$i,$this->y) )
				return $i - 1;
		}
		return $i-1;
	}
	/**
	 * Sets date to previous month
	 *
	 */
	public function previousMonth()
	{
		if( $this->m > 1 )
			$this->setDate( $this->y,$this->m-1,1);
		else
			$this->setDate( $this->y-1,12,1);
	}
	/**
	 * Sets the date to next month
	 *
	 */
	public function nextMonth()
	{
		if( $this->m > 11 )
			$this->setDate( $this->y+1,1,1);
		else
			$this->setDate( $this->y,$this->m+1,1);
	}
	/**
	 * returns a readable representation of the day
	 *
	 * @return String
	 */
	public function getReadableDay()
	{
		$CurrDate = getdate( $this->GetUnixTime() );
		return $this->LocaleDays[ $CurrDate["wday"] ];
	}
	/**
	 * returns a readable representation of the month
	 *
	 * @return String
	 */
	public function getReadableMonth()
	{
		$CurrDate = getdate( $this->GetUnixTime() );
		return $this->LocaleMonths[ $CurrDate["mon"]-1 ];
	}
	/**
	 * returns a readable representation of the date
	 *
	 * @param  String $Seperator
	 * @return String
	 */
	public function getSimpleDate( $seperator=".")
	{
		return $this->d.$seperator.$this->m.$seperator.$this->y;
	}
	/**
	 * returns a readable representation of the date
	 *	10 May 2006
	 *
	 * @return String
	 */
	public function getReadableDate()
	{
		return $this->d." ".$this->LocaleMonths[$this->m-1]." ".$this->y;
	}
	/**
	 * returns a readable representation of the date with day
	 *
	 *  10 May 2006 Monday
	 *
	 * @return String
	 */
	public function getReadableDateWithDay()
	{
		return $this->GetReadableDate()." ".$this->GetReadableDay();
	}

}
?>